package network;

import java.net.InetAddress;
import java.net.UnknownHostException;

import controller.ContactsTable;
import itdb1.commun.HeartBeats;

public class HeartBeatGenerator implements Runnable {
	
	private boolean isrunning;
	private ContactsTable contacts;
	
	public void run() {
		isrunning = true;
		contacts = ContactsTable.getInstance();
				
		while(isrunning)
		{
			try {
			    Thread.sleep(LocalNetworkSettings.LIFETIME / 2);
			} catch(InterruptedException e) {
			    e.printStackTrace();
			}
			
			try
			{
				new UDPTransmitter( new HeartBeats(contacts.getLocalusername(), InetAddress.getByName("255.255.255.255")), LocalNetworkSettings.REMOTE_UDP_SERVER_PORT);
			}
			catch(UnknownHostException e) {// On aura jamais cette erreur de toute façon
				e.printStackTrace();
			}
			
			contacts.removeOutDated();
		}
	}
	
	public void kill()
	{
		isrunning = false;
	}
	
}
